package com.macro.mall.portal.service.impl;

import com.github.pagehelper.PageHelper;
import com.macro.mall.mapper.PmsProductMapper;
import com.macro.mall.mapper.UmsMemberCollectionMapper;
import com.macro.mall.model.PmsProduct;
import com.macro.mall.model.UmsMember;
import com.macro.mall.model.UmsMemberCollection;
import com.macro.mall.model.UmsMemberCollectionExample;
import com.macro.mall.portal.service.MemberCollectionService;
import com.macro.mall.portal.service.UmsMemberService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 会员收藏Service实现类
 *
 * @author macro
 * @date 2018/8/2
 */
@Service
@AllArgsConstructor
public class MemberCollectionServiceImpl implements MemberCollectionService {
    private final PmsProductMapper productMapper;
    private final UmsMemberCollectionMapper umsMemberCollectionMapper;
    private final UmsMemberService memberService;

    @Override
    public int add(UmsMemberCollection productCollection) {
        int count = 0;
        if (productCollection.getProductId() == null) {
            return 0;
        }
        UmsMember member = memberService.getCurrentMember();
        productCollection.setMemberId(member.getId());
        productCollection.setMemberNickname(member.getNickname());
        productCollection.setMemberIcon(member.getIcon());
        UmsMemberCollectionExample example = new UmsMemberCollectionExample();
        example.createCriteria()
               .andMemberIdEqualTo(productCollection.getMemberId())
               .andProductIdEqualTo(productCollection.getProductId());
        List<UmsMemberCollection> umsMemberCollections = umsMemberCollectionMapper.selectByExample(example);
        if (!umsMemberCollections.isEmpty()) {
            PmsProduct product = productMapper.selectByPrimaryKey(productCollection.getProductId());
            if (product == null || product.getDeleteStatus() == 1) {
                return 0;
            }
            productCollection.setProductName(product.getName());
            productCollection.setProductSubTitle(product.getSubTitle());
            productCollection.setProductPrice(product.getPrice() + "");
            productCollection.setProductPic(product.getPic());
            count = umsMemberCollectionMapper.insert(productCollection);
        }
        return count;
    }

    @Override
    public int delete(Long productId) {
        UmsMember member = memberService.getCurrentMember();
        UmsMemberCollectionExample example = new UmsMemberCollectionExample();
        example.createCriteria()
               .andMemberIdEqualTo(member.getId())
               .andProductIdEqualTo(productId);
        return umsMemberCollectionMapper.deleteByExample(example);
    }

    @Override
    public List<UmsMemberCollection> list(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        UmsMember member = memberService.getCurrentMember();
        UmsMemberCollectionExample example = new UmsMemberCollectionExample();
        example.createCriteria()
               .andMemberIdEqualTo(member.getId());
        example.setOrderByClause("create_time desc");
        return umsMemberCollectionMapper.selectByExample(example);
    }

    @Override
    public UmsMemberCollection detail(Long productId) {
        UmsMember member = memberService.getCurrentMember();
        UmsMemberCollectionExample example = new UmsMemberCollectionExample();
        example.createCriteria()
               .andMemberIdEqualTo(member.getId())
               .andProductIdEqualTo(productId);
        List<UmsMemberCollection> memberCollectionList = umsMemberCollectionMapper.selectByExample(example);
        return memberCollectionList.isEmpty() ? null : memberCollectionList.get(0);
    }

    @Override
    public void clear() {
        UmsMember member = memberService.getCurrentMember();
        UmsMemberCollectionExample example = new UmsMemberCollectionExample();
        example.createCriteria()
               .andMemberIdEqualTo(member.getId());
        umsMemberCollectionMapper.deleteByExample(example);
    }
}
